-- Prepare to remove foreign keys
DECLARE @FkName  SYSNAME

-- Drop foreign keys for table 'AllDataTypes'
DECLARE fk_cursor CURSOR FOR 
  SELECT CONSTRAINT_NAME
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
   WHERE TABLE_SCHEMA = 'dbo'
     AND TABLE_NAME = 'AllDataTypes'
     AND CONSTRAINT_TYPE = 'FOREIGN KEY'
ORDER BY CONSTRAINT_NAME

OPEN fk_cursor
FETCH NEXT FROM fk_cursor INTO @FkName
WHILE @@FETCH_STATUS = 0
BEGIN
  EXEC('ALTER TABLE [AllDataTypes] DROP CONSTRAINT ' + @FkName)
  FETCH NEXT FROM fk_cursor INTO @FkName
END

CLOSE fk_cursor
DEALLOCATE fk_cursor
;

-- Drop foreign keys for table 'Departments'
DECLARE fk_cursor CURSOR FOR 
  SELECT CONSTRAINT_NAME
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
   WHERE TABLE_SCHEMA = 'dbo'
     AND TABLE_NAME = 'Departments'
     AND CONSTRAINT_TYPE = 'FOREIGN KEY'
ORDER BY CONSTRAINT_NAME

OPEN fk_cursor
FETCH NEXT FROM fk_cursor INTO @FkName
WHILE @@FETCH_STATUS = 0
BEGIN
  EXEC('ALTER TABLE [Departments] DROP CONSTRAINT ' + @FkName)
  FETCH NEXT FROM fk_cursor INTO @FkName
END

CLOSE fk_cursor
DEALLOCATE fk_cursor
;

-- Drop foreign keys for table 'Employees'
DECLARE fk_cursor CURSOR FOR 
  SELECT CONSTRAINT_NAME
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
   WHERE TABLE_SCHEMA = 'dbo'
     AND TABLE_NAME = 'Employees'
     AND CONSTRAINT_TYPE = 'FOREIGN KEY'
ORDER BY CONSTRAINT_NAME

OPEN fk_cursor
FETCH NEXT FROM fk_cursor INTO @FkName
WHILE @@FETCH_STATUS = 0
BEGIN
  EXEC('ALTER TABLE [Employees] DROP CONSTRAINT ' + @FkName)
  FETCH NEXT FROM fk_cursor INTO @FkName
END

CLOSE fk_cursor
DEALLOCATE fk_cursor
;

-- Drop foreign keys for table 'ItemBranches'
DECLARE fk_cursor CURSOR FOR 
  SELECT CONSTRAINT_NAME
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
   WHERE TABLE_SCHEMA = 'dbo'
     AND TABLE_NAME = 'ItemBranches'
     AND CONSTRAINT_TYPE = 'FOREIGN KEY'
ORDER BY CONSTRAINT_NAME

OPEN fk_cursor
FETCH NEXT FROM fk_cursor INTO @FkName
WHILE @@FETCH_STATUS = 0
BEGIN
  EXEC('ALTER TABLE [ItemBranches] DROP CONSTRAINT ' + @FkName)
  FETCH NEXT FROM fk_cursor INTO @FkName
END

CLOSE fk_cursor
DEALLOCATE fk_cursor
;

-- Drop foreign keys for table 'Items'
DECLARE fk_cursor CURSOR FOR 
  SELECT CONSTRAINT_NAME
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
   WHERE TABLE_SCHEMA = 'dbo'
     AND TABLE_NAME = 'Items'
     AND CONSTRAINT_TYPE = 'FOREIGN KEY'
ORDER BY CONSTRAINT_NAME

OPEN fk_cursor
FETCH NEXT FROM fk_cursor INTO @FkName
WHILE @@FETCH_STATUS = 0
BEGIN
  EXEC('ALTER TABLE [Items] DROP CONSTRAINT ' + @FkName)
  FETCH NEXT FROM fk_cursor INTO @FkName
END

CLOSE fk_cursor
DEALLOCATE fk_cursor
;

-- Drop foreign keys for table 'TestForeignKeyOptions'
DECLARE fk_cursor CURSOR FOR 
  SELECT CONSTRAINT_NAME
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
   WHERE TABLE_SCHEMA = 'dbo'
     AND TABLE_NAME = 'TestForeignKeyOptions'
     AND CONSTRAINT_TYPE = 'FOREIGN KEY'
ORDER BY CONSTRAINT_NAME

OPEN fk_cursor
FETCH NEXT FROM fk_cursor INTO @FkName
WHILE @@FETCH_STATUS = 0
BEGIN
  EXEC('ALTER TABLE [TestForeignKeyOptions] DROP CONSTRAINT ' + @FkName)
  FETCH NEXT FROM fk_cursor INTO @FkName
END

CLOSE fk_cursor
DEALLOCATE fk_cursor
;

-- Drop foreign keys for table 'TestForeignKeyOptions2'
DECLARE fk_cursor CURSOR FOR 
  SELECT CONSTRAINT_NAME
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
   WHERE TABLE_SCHEMA = 'dbo'
     AND TABLE_NAME = 'TestForeignKeyOptions2'
     AND CONSTRAINT_TYPE = 'FOREIGN KEY'
ORDER BY CONSTRAINT_NAME

OPEN fk_cursor
FETCH NEXT FROM fk_cursor INTO @FkName
WHILE @@FETCH_STATUS = 0
BEGIN
  EXEC('ALTER TABLE [TestForeignKeyOptions2] DROP CONSTRAINT ' + @FkName)
  FETCH NEXT FROM fk_cursor INTO @FkName
END

CLOSE fk_cursor
DEALLOCATE fk_cursor
;

-- Drop foreign keys for table 'TestForeignKeyOptions3'
DECLARE fk_cursor CURSOR FOR 
  SELECT CONSTRAINT_NAME
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
   WHERE TABLE_SCHEMA = 'dbo'
     AND TABLE_NAME = 'TestForeignKeyOptions3'
     AND CONSTRAINT_TYPE = 'FOREIGN KEY'
ORDER BY CONSTRAINT_NAME

OPEN fk_cursor
FETCH NEXT FROM fk_cursor INTO @FkName
WHILE @@FETCH_STATUS = 0
BEGIN
  EXEC('ALTER TABLE [TestForeignKeyOptions3] DROP CONSTRAINT ' + @FkName)
  FETCH NEXT FROM fk_cursor INTO @FkName
END

CLOSE fk_cursor
DEALLOCATE fk_cursor
;

-- Drop foreign keys for table 'TestForeignKeyOptions4'
DECLARE fk_cursor CURSOR FOR 
  SELECT CONSTRAINT_NAME
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
   WHERE TABLE_SCHEMA = 'dbo'
     AND TABLE_NAME = 'TestForeignKeyOptions4'
     AND CONSTRAINT_TYPE = 'FOREIGN KEY'
ORDER BY CONSTRAINT_NAME

OPEN fk_cursor
FETCH NEXT FROM fk_cursor INTO @FkName
WHILE @@FETCH_STATUS = 0
BEGIN
  EXEC('ALTER TABLE [TestForeignKeyOptions4] DROP CONSTRAINT ' + @FkName)
  FETCH NEXT FROM fk_cursor INTO @FkName
END

CLOSE fk_cursor
DEALLOCATE fk_cursor
;

-- Drop foreign keys for table 'ZipCodes'
DECLARE fk_cursor CURSOR FOR 
  SELECT CONSTRAINT_NAME
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
   WHERE TABLE_SCHEMA = 'dbo'
     AND TABLE_NAME = 'ZipCodes'
     AND CONSTRAINT_TYPE = 'FOREIGN KEY'
ORDER BY CONSTRAINT_NAME

OPEN fk_cursor
FETCH NEXT FROM fk_cursor INTO @FkName
WHILE @@FETCH_STATUS = 0
BEGIN
  EXEC('ALTER TABLE [ZipCodes] DROP CONSTRAINT ' + @FkName)
  FETCH NEXT FROM fk_cursor INTO @FkName
END

CLOSE fk_cursor
DEALLOCATE fk_cursor
;

-- Drop table 'AllDataTypes'
IF EXISTS (
  SELECT * FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_TYPE = 'BASE TABLE'
  AND TABLE_SCHEMA = 'dbo'
  AND TABLE_NAME = 'AllDataTypes'
  )
BEGIN
  DROP TABLE [dbo].[AllDataTypes]
END
;

-- Drop table 'Departments'
IF EXISTS (
  SELECT * FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_TYPE = 'BASE TABLE'
  AND TABLE_SCHEMA = 'dbo'
  AND TABLE_NAME = 'Departments'
  )
BEGIN
  DROP TABLE [dbo].[Departments]
END
;

-- Drop table 'Employees'
IF EXISTS (
  SELECT * FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_TYPE = 'BASE TABLE'
  AND TABLE_SCHEMA = 'dbo'
  AND TABLE_NAME = 'Employees'
  )
BEGIN
  DROP TABLE [dbo].[Employees]
END
;

-- Drop table 'ItemBranches'
IF EXISTS (
  SELECT * FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_TYPE = 'BASE TABLE'
  AND TABLE_SCHEMA = 'dbo'
  AND TABLE_NAME = 'ItemBranches'
  )
BEGIN
  DROP TABLE [dbo].[ItemBranches]
END
;

-- Drop table 'Items'
IF EXISTS (
  SELECT * FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_TYPE = 'BASE TABLE'
  AND TABLE_SCHEMA = 'dbo'
  AND TABLE_NAME = 'Items'
  )
BEGIN
  DROP TABLE [dbo].[Items]
END
;

-- Drop table 'TestForeignKeyOptions'
IF EXISTS (
  SELECT * FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_TYPE = 'BASE TABLE'
  AND TABLE_SCHEMA = 'dbo'
  AND TABLE_NAME = 'TestForeignKeyOptions'
  )
BEGIN
  DROP TABLE [dbo].[TestForeignKeyOptions]
END
;

-- Drop table 'TestForeignKeyOptions2'
IF EXISTS (
  SELECT * FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_TYPE = 'BASE TABLE'
  AND TABLE_SCHEMA = 'dbo'
  AND TABLE_NAME = 'TestForeignKeyOptions2'
  )
BEGIN
  DROP TABLE [dbo].[TestForeignKeyOptions2]
END
;

-- Drop table 'TestForeignKeyOptions3'
IF EXISTS (
  SELECT * FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_TYPE = 'BASE TABLE'
  AND TABLE_SCHEMA = 'dbo'
  AND TABLE_NAME = 'TestForeignKeyOptions3'
  )
BEGIN
  DROP TABLE [dbo].[TestForeignKeyOptions3]
END
;

-- Drop table 'TestForeignKeyOptions4'
IF EXISTS (
  SELECT * FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_TYPE = 'BASE TABLE'
  AND TABLE_SCHEMA = 'dbo'
  AND TABLE_NAME = 'TestForeignKeyOptions4'
  )
BEGIN
  DROP TABLE [dbo].[TestForeignKeyOptions4]
END
;

-- Drop table 'ZipCodes'
IF EXISTS (
  SELECT * FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_TYPE = 'BASE TABLE'
  AND TABLE_SCHEMA = 'dbo'
  AND TABLE_NAME = 'ZipCodes'
  )
BEGIN
  DROP TABLE [dbo].[ZipCodes]
END
;

